//
//  SLMineCancellationVC.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/19.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import UIKit

class SLMineCancellationVC: SLBaseVC {
    var handle:SLBlock?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Account Cancellation"
        
        view.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(self.navigationView.snp.bottom).offset(32)
            make.height.equalTo(180)
        }
        
        view.addSubview(warningView)
        
        
        
        warningView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(textView.snp.bottom).offset(12)
            make.height.equalTo(48)
        }
        warningButton.isEnabled = false
        warningButton.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        view.addSubview(logoutButton)
        
        logoutButton.snp.makeConstraints { make in
            make.width.equalTo(281)
            make.height.equalTo(44)
            make.bottom.equalTo(-48-DevConfig.safe_bottom)
            make.centerX.equalToSuperview()
        }
        
        let agreementStack = UIStackView(arrangedSubviews: [agreementCheckbox, agreementLabel])
        agreementStack.spacing = 8
        agreementStack.alignment = .center
        agreementStack.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(agreementStack)
        agreementStack.snp.makeConstraints { make in
            make.top.equalTo(logoutButton.snp.bottom).offset(6.5)
            make.centerX.equalToSuperview()
        }
    }
    lazy var textView:UITextView = {
        let tv = UITextView()
        tv.font = SLFontConfig.regular15
        let text = "Account cannot be recovered after cancellation. To ensure the security of your account please confrm that account related services have been handled properly before application and pay attention to the following conditions:"
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 12 // 设置行间距
        paragraphStyle.paragraphSpacing = 12 // 设置段落间距
        let attributes:[NSAttributedString.Key : Any] = [
            NSAttributedString.Key.font : SLFontConfig.regular15
                                     ]
        let attributedString = NSMutableAttributedString(string: text ,attributes: attributes)
        attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
        tv.textContainerInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
        tv.attributedText = attributedString
        tv.isEditable = false
        tv.layer.masksToBounds = true
        tv.backgroundColor = rgba(hex: "#F3F6F9")
        tv.layer.cornerRadius = 10
        return tv
    }()
    lazy var warningView:UIView = {
        let v = UIView()
        v.backgroundColor = rgba(hex: "#F3F6F9")
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 10
        
        return v
    }()
    lazy var warningButton:UIButton = {
        let v = create_button(title: "  All loans are settled", titleColor: rgba(hex: "#FF461B"), titleFont: SLFontConfig.regular15, image: IMAGE("icon_warn"), supView: warningView, closure: { make in
        }, selector: nil)
        return v
    }()
    
    lazy var logoutButton: UIButton = {
       let button = UIButton(type: .system)
       button.setTitle("Apply Now", for: .normal)
       button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
       button.setTitleColor(.white, for: .normal)
       button.backgroundColor = rgba(hex: "#747474", a: 0.24)
       button.layer.cornerRadius = 22
       button.isEnabled = false
       button.addTarget(self, action: #selector(logoutAction), for: .touchUpInside)
       button.translatesAutoresizingMaskIntoConstraints = false
       return button
      }()
    
    lazy var agreementCheckbox: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(IMAGE("signin_icon_select"), for: .normal)
        button.setImage(IMAGE("signin_icon_selected"), for: .selected)
//        button.tintColor =  rgba(hex: "#1608EC")
        button.translatesAutoresizingMaskIntoConstraints = false
        button.addTarget(self, action: #selector(agreementCheckboxTapped), for: .touchUpInside)
        return button
       }()
       
    lazy var agreementLabel: UILabel = {
           let label = UILabel()
           let text = "I have read and agree Loan Agreement"
           label.text = text
           label.font = UIFont.systemFont(ofSize: 14)
           label.textColor = .gray
           label.translatesAutoresizingMaskIntoConstraints = false
           
           // 设置部分文本带下划线
           let attributedString = NSMutableAttributedString(string: "I have read and agree Loan Agreement")
           let range = text.range(of: "Loan Agreement")
           let startIndex = text.distance(from: text.startIndex, to: range!.lowerBound)
            let length = range!.upperBound.utf16Offset(in: text) - range!.lowerBound.utf16Offset(in: text)
           let rangeNs = NSRange(location: startIndex, length: length)

           attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: rangeNs)
           attributedString.addAttribute(.foregroundColor, value: rgba(hex: "#1608EC"), range: rangeNs)
           
           label.attributedText = attributedString
           // 添加点击手势
           label.isUserInteractionEnabled = true
           let tapGesture = UITapGestureRecognizer(target: self, action: #selector(agreementClick))
           label.addGestureRecognizer(tapGesture)
           
           return label
       }()
       
    @objc private func agreementCheckboxTapped() {
        agreementCheckbox.isSelected = !agreementCheckbox.isSelected
        updateLoginButtonState()
    }
    @objc private func logoutAction(){
        if let block = self.handle{
            block(0)
        }
        self.navigationController?.popViewController(animated: true)
    }
    private func updateLoginButtonState() {
        logoutButton.isEnabled = agreementCheckbox.isSelected
        
        
        logoutButton.backgroundColor = agreementCheckbox.isSelected ?  rgba(hex: "#1608EC") : rgba(hex: "#747474", a: 0.24)
    }
    @objc private func agreementClick(){
        let url = __valueForKey(key: KProtocol)
        
        jump(url: url as! String)
        
    }
    class func push(page:UIViewController,handle:SLBlock?){
        let alert = SLMineCancellationVC()
        alert.modalPresentationStyle = .overCurrentContext
        alert.modalTransitionStyle = .crossDissolve
        alert.handle = handle
        page.navigationController?.pushViewController(alert, animated: true)
    }

}
